#ifndef UHM_MAT_EXTENSION_LDL_PIV_HXX_
#define UHM_MAT_EXTENSION_LDL_PIV_HXX_

namespace uhm {
  
  // Basic LDL piv...
  template<class A_, class B_>
  class LDL_piv_ : public LDL_nopiv_<A_,B_> {
  private:
  protected:
  public:

    LDL_piv_() : LDL_nopiv_<A_,B_>() { }
    LDL_piv_(Int_ type, Int_ uplo, Int_ sym,
             Int_ mt, Int_ mb,
             Int_ rhs, Int_ blk) 
      : LDL_nopiv_<A_,B_>(type, mt, mb, rhs, blk) { }
    LDL_piv_(Int_ type, 
             Int_ mt, Int_ mb, 
             Int_ rhs, 
             Int_ blk)  
      : LDL_nopiv_<A_,B_>(type, mt, mb, rhs, blk) { }
    
    LDL_piv_(const LDL_piv_ &b) : LDL_nopiv_<A_,B_>(b) { }

    using LDL_nopiv_<A_,B_>::get_leading_flop;
	  
    using LDL_nopiv_<A_,B_>::decompose;
    using LDL_nopiv_<A_,B_>::solve_1_x;
    using LDL_nopiv_<A_,B_>::solve_2_x;

    virtual Int_ update() {

      if (this->get_mt() && this->get_mb()) {
        apply_pivots( Mat_Base_::RIGHT, Mat_Base_::TRANSPOSE, 
                      this->_P, this->_ABL );

        trsm( Mat_Base_::RIGHT, Mat_Base_::UPPER_TRIANGULAR, 
              Mat_Base_::NO_TRANSPOSE, Mat_Base_::NONUNIT_DIAG,
              1, this->_ATL, this->_ABL );

        this->_ATR.create_flat_buffer();
        copyt( this->_ABL, this->_ATR );
        apply_diag_scal( Mat_Base_::LEFT, Mat_Base_::NO_CONJUGATE, 
                         this->_ATL, this->_ATR );

#pragma omp taskwait

        gemm_l( Mat_Base_::NO_TRANSPOSE, Mat_Base_::NO_TRANSPOSE,
                -1, this->_ABL, this->_ATR, 1, this->_ABR );

        this->_ATR.free_flat_buffer();
      }
      return true;
    }
    
    virtual Int_ decompose() {
      if (this->get_mt()) {

        // Do LDL but resulting matrix is LU
        ldl_piv( Mat_Base_::LOWER_TRIANGULAR, this->_ATL, this->_P );
        this->_ATL.hermitianize();
        apply_diag_scal( Mat_Base_::LEFT, Mat_Base_::NO_CONJUGATE,
                         Mat_Base_::UPPER_TRIANGULAR,
                         this->_ATL );
      }

      this->update();

      return true;
    }

    virtual Int_ solve_1_x() {
      if (this->get_mt()) {

        apply_pivots( Mat_Base_::LEFT, Mat_Base_::NO_TRANSPOSE, 
                      this->_P, this->_XT );
        trsm( Mat_Base_::LEFT, Mat_Base_::LOWER_TRIANGULAR,
              Mat_Base_::NO_TRANSPOSE, Mat_Base_::UNIT_DIAG,
              1, this->_ATL, this->_XT );
      }
      if (this->get_mt() && this->get_mb()) {
        gemm( Mat_Base_::NO_TRANSPOSE, Mat_Base_::NO_TRANSPOSE,
              -1, this->_ABL, this->_XT, 1, this->_XB );
      }
      return true;
    }

    virtual Int_ solve_2_x() {
      if (this->get_mb() && this->get_mt()) {

        this->_ATR.create_flat_buffer();

        copyt( this->_ABL, this->_ATR );
        apply_diag_scal( Mat_Base_::LEFT, Mat_Base_::NO_CONJUGATE,
                         this->_ATL, this->_ATR );

        gemm( Mat_Base_::NO_TRANSPOSE, Mat_Base_::NO_TRANSPOSE,
              -1, this->_ATR, this->_XB, 1, this->_XT );

        this->_ATR.free_flat_buffer();
      }

      if (this->get_mt()) {
        trsm( Mat_Base_::LEFT, Mat_Base_::UPPER_TRIANGULAR,
              Mat_Base_::NO_TRANSPOSE, Mat_Base_::NONUNIT_DIAG, 
              1, this->_ATL,  this->_XT );
        apply_pivots( Mat_Base_::LEFT, Mat_Base_::TRANSPOSE,
                      this->_P, this->_XT );
      }
      return true;
    }

    virtual Int_ solve_1_r() {
      if (this->get_mt()) {

        apply_pivots( Mat_Base_::LEFT, Mat_Base_::NO_TRANSPOSE, 
                      this->_P, this->_RT );
        trsm( Mat_Base_::LEFT, Mat_Base_::LOWER_TRIANGULAR,
              Mat_Base_::NO_TRANSPOSE, Mat_Base_::UNIT_DIAG,
              1, this->_ATL, this->_RT );
      }
      if (this->get_mt() && this->get_mb()) {
        gemm( Mat_Base_::NO_TRANSPOSE, Mat_Base_::NO_TRANSPOSE,
              -1, this->_ABL, this->_RT, 1, this->_RB );
      }
      return true;
    }

    virtual Int_ solve_2_r() {
      if (this->get_mb() && this->get_mt()) {

        this->_ATR.create_flat_buffer();

        copyt( this->_ABL, this->_ATR );
        apply_diag_scal( Mat_Base_::LEFT, Mat_Base_::NO_CONJUGATE,
                         this->_ATL, this->_ATR );

        gemm( Mat_Base_::NO_TRANSPOSE, Mat_Base_::NO_TRANSPOSE,
              -1, this->_ATR, this->_RB, 1, this->_RT );

        this->_ATR.free_flat_buffer();
      }

      if (this->get_mt()) {
        trsm( Mat_Base_::LEFT, Mat_Base_::UPPER_TRIANGULAR,
              Mat_Base_::NO_TRANSPOSE, Mat_Base_::NONUNIT_DIAG, 
              1, this->_ATL,  this->_RT );
        apply_pivots( Mat_Base_::LEFT, Mat_Base_::TRANSPOSE,
                      this->_P, this->_RT );
      }
      return true;
    }

  };

}

#endif
